package com.gxa.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.common.commons.R;
import com.gxa.order.mapper.*;
import com.gxa.order.pojo.*;
import com.gxa.order.service.FinancialMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class FinancialMsgServiceImpl implements FinancialMsgService {
    @Autowired
    private FinancialMsgMapper financialMsgMapper;

    @Autowired
    private MpayMapper mpayMapper;

    @Autowired
    private MmonryMapper mmonryMapper;

    @Autowired
    private MpactMapper mpactMapper;

    @Autowired
    private GenJinMapper genJinMapper;

    @Autowired
    private TiXinMapper tiXinMapper;


    @Override
    public R findshoufukuan(String pay_id) {
        List<Map> findshoufukuan = financialMsgMapper.findshoufukuan(pay_id);
        R r = new R();
        if(findshoufukuan!=null){
           r.setCode(200);
           r.setMsg("操作成功");
           r.setData(findshoufukuan);
        }
        return r;
    }

    @Override
    public R addshoufukuan(Mpay mpay, Mmoney mmoney) {



//        创建收付款是随机生成付款编号
        String payid = "7890";
        mpay.setPayId(payid);
        mmoney.setPayId(payid);
        if(mmoney.getPayMathod().equals("线下支付")){

//        判断是否上传了支付凭证，如果上传了支付凭证就把状态改为待审核否则就是待上传凭证
            if(mmoney.getMfile()==null){
                mmoney.setDstatus((byte) 3);//待上传凭证
            }else {
                mmoney.setDstatus((byte) 4);//待审核
            }
        }

        int insert = mpayMapper.insert(mpay);
        int insert1 = mmonryMapper.insert(mmoney);
        R r = new R();
        if(insert>0&&insert1>0){
            r.setCode(200);
            r.setMsg("操作成功");

        }


        return r;
    }

    @Override
    public R updateshoufukuan(Mpay mpay, Mmoney mmoney) {
        if(mmoney.getPayMathod().equals("线下支付")){

//        判断是否上传了支付凭证，如果上传了支付凭证就把状态改为待审核否则就是待上传凭证
//            如果编辑的时候还是没有上传凭证，并且原来的状态就是待上传，那么本条记录状态依旧是待上传
//            否则就是待审核
            if(mmoney.getMfile()==null&&mmoney.getDstatus()==3){
                mmoney.setDstatus((byte) 3);//待上传凭证
            }else {
                mmoney.setDstatus((byte) 4);//待审核
            }
        }
        QueryWrapper<Mpay> queryWrapper = new QueryWrapper();
        queryWrapper.eq("pay_Id",mpay.getPayId());
        QueryWrapper<Mmoney> queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("pay_Id",mmoney.getPayId());
        int i = mpayMapper.update(mpay, queryWrapper);
        int i1 = mmonryMapper.update(mmoney, queryWrapper1);
        R r = new R();
        if(i>0&&i1>0){
            r.setCode(200);
            r.setMsg("操作成功");

        }

        return r;
    }

    @Override
    public R addhetong(Mpay mpay, Mmoney mmoney, Mpact mpact) {
//        创建合同id
        String pactid = "qee";
        mpact.setPactId(pactid);
        mpay.setPactId(pactid);
//        创建收付款是随机生成付款编号
        String payid = "1314";
        mpay.setPayId(payid);
        mmoney.setPayId(payid);
        if(mmoney.getPayMathod().equals("线下支付")){

//        判断是否上传了支付凭证，如果上传了支付凭证就把状态改为待审核否则就是待上传凭证
            if(mmoney.getMfile()==null){
                mmoney.setDstatus((byte) 3);//待上传凭证
            }else {
                mmoney.setDstatus((byte) 4);//待审核
            }
        }
        int insert2 = mpactMapper.insert(mpact);
        int insert = mpayMapper.insert(mpay);
        int insert1 = mmonryMapper.insert(mmoney);
        R r = new R();
        if(insert>0&&insert1>0&&insert2>0){
            r.setCode(200);
            r.setMsg("操作成功");

        }

        return r;
    }

    @Override
    public R updatehetong(Mpay mpay, Mmoney mmoney, Mpact mpact) {
        if(mmoney.getPayMathod().equals("线下支付")){

//        判断是否上传了支付凭证，如果上传了支付凭证就把状态改为待审核否则就是待上传凭证
//            如果编辑的时候还是没有上传凭证，并且原来的状态就是待上传，那么本条记录状态依旧是待上传
//            否则就是待审核
            if(mmoney.getMfile()==null&&mmoney.getDstatus()==3){
                mmoney.setDstatus((byte) 3);//待上传凭证
            }else {
                mmoney.setDstatus((byte) 4);//待审核
            }
        }
        QueryWrapper<Mpact> queryWrapper2 = new QueryWrapper();
        queryWrapper2.eq("pact_id",mpact.getPactId());
        QueryWrapper<Mpay> queryWrapper = new QueryWrapper();
        queryWrapper.eq("pay_id",mpay.getPayId());
        QueryWrapper<Mmoney> queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("pay_id",mmoney.getPayId());
        int i2 = mpactMapper.update(mpact, queryWrapper2);
        int i = mpayMapper.update(mpay, queryWrapper);
        int i1 = mmonryMapper.update(mmoney, queryWrapper1);
        R r = new R();
        if(i>0&&i1>0&&i2>0){
            r.setCode(200);
            r.setMsg("操作成功");

        }

        return r;



    }

    @Override
    public R findhetong(String pact_id,String pay_id) {
        List<Map> findhetong = financialMsgMapper.findhetong(pact_id,pay_id);
        R r = new R();
        if(findhetong!=null){
            r.setCode(200);
            r.setMsg("操作成功");
            r.setData(findhetong);
        }

        return r;
    }

    @Override
    public R findgenjin(Integer currentPage,Integer pageSize) {
        Page<GenJin> page = new Page(currentPage,pageSize);
        Page<GenJin> genJinPage = genJinMapper.selectPage(page, null);
        R r = new R();
        if(genJinPage!=null){
            r.setCode(200);
            r.setMsg("操作成功");
            r.setData(genJinPage);
        }
        return r;
    }

    @Override
    public R addgenjin(GenJin genJin) {
        int insert = genJinMapper.insert(genJin);
        R r = new R();
        if(insert>0){
            r.setCode(200);
            r.setMsg("操作成功");

        }

        return r;
    }

    @Override
    public R updategenjin(GenJin genJin) {

        int i = genJinMapper.updateById(genJin);
        R r = new R();
        if(i>0){
            r.setCode(200);
            r.setMsg("操作成功");

        }

        return r;
    }

    @Override
    public R findtixin(Integer currentPage, Integer pageSize) {
        currentPage = (currentPage-1)*pageSize;
        Page<TiXin> page = new Page(currentPage,pageSize);
        Page<TiXin> tiXinPage = tiXinMapper.selectPage(page, null);
        R r = new R();
        if(tiXinPage!=null){
            r.setCode(200);
            r.setMsg("操作成功");
            r.setData(tiXinPage);
        }
        return r;
    }

    @Override
    public R addtixin(TiXin tiXin) {
        int insert = tiXinMapper.insert(tiXin);
        R r = new R();
        if(insert>0){
            r.setCode(200);
            r.setMsg("操作成功");
            r.setData(insert);
        }

        return r;
    }

    @Override
    public R updatetixin(TiXin tiXin) {
        int i = tiXinMapper.updateById(tiXin);
        R r = new R();
        if(i>0){
            r.setCode(200);
            r.setMsg("操作成功");
            r.setData(i);
        }
        return r;
    }

    @Override
    public R findshoufukuanAll(Integer currentPage, Integer pageSize,String clean_order_number) {
        currentPage = (currentPage-1)*pageSize;
        List<Map> maps = financialMsgMapper.findshoufukuanAll(currentPage, pageSize,clean_order_number);
        R r = new R();
        if(maps!=null){
            r.setCode(200);
            r.setMsg("操作成功");
            r.setData(maps);
        }
        return r;
    }

    @Override
    public R findhetongAll(Integer currentPage, Integer pageSize,String clean_order_number) {
        currentPage = (currentPage-1)*pageSize;
        List<Map> maps = financialMsgMapper.findhetongAll(currentPage, pageSize,clean_order_number);
        R r = new R();
        if(maps!=null){
            r.setCode(200);
            r.setMsg("操作成功");
            r.setData(maps);
        }
        return r;
    }
}
